﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Media;

namespace JustAMinute
{
    public partial class MenuForm : Form
    {
        private string themeName;
        private Color formColor;
        private GameForm gameForm;
        private SoundPlayer menuSoundPlayer;
        private LevelConfig[] levelConfig;
        public MenuForm()
        {
            InitializeComponent();

            this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.BackColor = Color.Brown;
            this.Text = "Just A Minute";

            chooseWallpaperLabel.Visible = false;
            chooseLevelLabel.Visible = false;
            weatherButton.Visible = false;
            animalButton.Visible = false;
            natureButton.Visible = false;
            gameheroButton.Visible = false;
            levelBar.Visible = false;
            level1Label.Visible = false;
            level2Label.Visible = false;
            level3Label.Visible = false;
            level4Label.Visible = false;
            level5Label.Visible = false;
            startGameButton.Visible = false;
            resumeButton.Visible = false;

            newGameButton.BackColor = Color.LightSkyBlue;
            settingsButton.BackColor = Color.LightSkyBlue;
            quitButton.BackColor = Color.LightSkyBlue;
            resumeButton.BackColor = Color.LightSkyBlue;
            startGameButton.BackColor = Color.LightSkyBlue;

            
            weatherButton.TextAlign = ContentAlignment.TopLeft;
            animalButton.TextAlign = ContentAlignment.TopLeft;
            natureButton.TextAlign = ContentAlignment.TopLeft;
            gameheroButton.TextAlign = ContentAlignment.TopLeft;

            
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string fullWeatherName = Path.Combine(directory, "DataStore/Images/ThemePicture/weatherTheme.png");
            string fullAnimalName = Path.Combine(directory, "DataStore/Images/ThemePicture/animalTheme.jpg");
            string fullNatureName = Path.Combine(directory, "DataStore/Images/ThemePicture/natureTheme.jpg");
            string fullGameHeroName = Path.Combine(directory, "DataStore/Images/ThemePicture/gameHeroTheme.jpg");
            
           
            weatherButton.BackgroundImage = Image.FromFile(fullWeatherName);
            animalButton.BackgroundImage = Image.FromFile(fullAnimalName);
            natureButton.BackgroundImage = Image.FromFile(fullNatureName);
            gameheroButton.BackgroundImage = Image.FromFile(fullGameHeroName);

            levelConfig = new LevelConfig[5];
            levelConfig[0] = new LevelConfig(12,60);
            levelConfig[1] = new LevelConfig(12, 40);
            levelConfig[2] = new LevelConfig(12, 30);
            levelConfig[3] = new LevelConfig(16, 60);
            levelConfig[4] = new LevelConfig(16, 50);
           

         

            menuSoundPlayer = new SoundPlayer(Path.Combine(directory, "DataStore/Sounds/menuButtonSound.wav"));
            
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            menuTimer.Interval = 100;
            menuTimer.Start();
            menuTimer.Tick += ShowHideFormBorders;
        }

        private void ShowHideFormBorders(object sender, EventArgs e)
        {

            int yPosition = Cursor.Position.Y;

            if (yPosition > 20)
            {
                this.FormBorderStyle = FormBorderStyle.None;
            }

            else
            {
                this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            }
        }
    
        private void quitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void newGameButton_Click(object sender, EventArgs e)
        {
            SetMenuElemetsVisibility(false);
            SetThemeElementsVisibility(true);  
        }

        private void weatherButton_Click(object sender, EventArgs e)
        {
            SetThemeElementsVisibility(false);
            SetLevelElementsVisibility(true);
            themeName = "Weather";
            formColor = Color.CornflowerBlue;
     
        }

        private void animalButton_Click(object sender, EventArgs e)
        {
            SetThemeElementsVisibility(false);
            SetLevelElementsVisibility(true);
            themeName = "Animals";
            formColor = Color.PaleVioletRed;
        }

        private void natureButton_Click(object sender, EventArgs e)
        {
            SetThemeElementsVisibility(false);
            SetLevelElementsVisibility(true);
            themeName = "Nature";
            formColor = Color.SaddleBrown;
        }

        private void gameheroButton_Click(object sender, EventArgs e)
        {
            SetThemeElementsVisibility(false);
            SetLevelElementsVisibility(true);
            themeName = "GameHero";
            formColor = Color.LightSeaGreen;
        }
        private void SetMenuElemetsVisibility(bool b)
        {
            menuPicture.Visible = b;
            newGameButton.Visible = b;
            settingsButton.Visible = b;
            quitButton.Visible = b;
        }

        private void SetThemeElementsVisibility(bool b)
        {
            chooseWallpaperLabel.Visible = b;
            weatherButton.Visible = b;
            animalButton.Visible = b;
            natureButton.Visible = b;
            gameheroButton.Visible = b;
        }
        private void SetLevelElementsVisibility(bool b)
        {
            chooseLevelLabel.Visible = b;
            levelBar.Visible = b;

            level1Label.Visible = b;
            level2Label.Visible = b;
            level3Label.Visible = b;
            level4Label.Visible = b;
            level5Label.Visible = b;
            startGameButton.Visible = b;
        }

        private void startGameButton_Click(object sender, EventArgs e)
        {
            

            
           Level level = new Level(levelConfig[levelBar.Value - 1], themeName,levelBar.Value.ToString(),formColor);
           


            gameForm = new GameForm(level);
            gameForm.Show();
            resumeButton.Visible = true;

            SetMenuElemetsVisibility(true);
            SetThemeElementsVisibility(false);
            SetLevelElementsVisibility(false);
            menuTimer.Tick += (obj, z) =>
            {
                if(gameForm.IsDisposed)
                {
                    resumeButton.Visible = false;
                }
            };
        }

        private void resumeButton_Click(object sender, EventArgs e)
        {
            gameForm.Show();
            resumeButton.Visible = false;
            
        }

        private void newGameButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
            newGameButton.BackColor = Color.CadetBlue;
        }

        private void newGameButton_MouseLeave(object sender, EventArgs e)
        {
            newGameButton.BackColor = Color.LightSkyBlue;
        }

        private void settingsButton_Click(object sender, EventArgs e)
        {
           
        }
        private void settingsButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
            settingsButton.BackColor = Color.CadetBlue;
        }
        private void settingsButton_MouseLeave(object sender, EventArgs e)
        {
            settingsButton.BackColor = Color.LightSkyBlue;
        }

        private void quitButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
            quitButton.BackColor = Color.CadetBlue;
        }

        private void resumeButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
            resumeButton.BackColor = Color.CadetBlue;
        }

        private void resumeButton_MouseLeave(object sender, EventArgs e)
        {
            resumeButton.BackColor = Color.LightSkyBlue;
        }

        private void quitButton_MouseLeave(object sender, EventArgs e)
        {
            quitButton.BackColor = Color.LightSkyBlue;
        }

        private void weatherButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
            weatherButton.BackColor = Color.Black;
        }

        private void animalButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
        }

        private void natureButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
        }

        private void gameheroButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
        }

        private void startGameButton_MouseEnter(object sender, EventArgs e)
        {
            menuSoundPlayer.Play();
            startGameButton.BackColor = Color.CadetBlue;
        }

        private void startGameButton_MouseLeave(object sender, EventArgs e)
        {
            startGameButton.BackColor = Color.LightSkyBlue;
        }

        private void gameNameLabel_Click(object sender, EventArgs e)
        {

        }

    }
}
